package cn.lena.idle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.lena.idle.adapter.MyCollectionAdapter;
import cn.lena.idle.dao.CollectionDbHelper;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.DTO.CurrentGoods;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;

/**
 * @anthor lena
 * @date 2021/6/11
 */
public class MyCollectionActivity extends AppCompatActivity {

    private ListView lv;
    private ImageView back;
    private MyCollectionAdapter adapter;
    private CollectionDbHelper collection_db;
    private GoodsDbHelper goods_db;
    private List<Goods> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);

        adapter = new MyCollectionAdapter(getApplicationContext());
        lv=(ListView)findViewById(R.id.collectionlv);
        back=(ImageView)findViewById(R.id.returnbefore3);

        // 初始化：查询用户收藏列表 t
        lists=getMyCollectionGoods();
        if(lists != null) {
            adapter.setData(lists);
            lv.setAdapter(adapter);
        }
        else{
            Toast.makeText(this,"没有收藏的商品",Toast.LENGTH_LONG).show();
        }

        // 单击某个商品 进入商品详情页 t
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Goods current = (Goods)adapter.getItem(position);
                toDetail(current);
            }
        });


        // 长按取消收藏 t
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                cancelCollection(position);
                // 返回值如果是false，执行完长按事件还会执行单击事件；如果是true，执行完长按事件不会再执行单击事件。
                return true;
            }
        });

        // 返回上一页 t
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyCollectionActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 单击跳转到商品详情页面 t
     */
    public void toDetail(Goods goods){
        CurrentGoods.SELECT=goods;
        Intent intent=new Intent(MyCollectionActivity.this, GoodsDetailActivity.class);
        startActivity(intent);
    }


    /**
     * 长按菜单项取消收藏 t
     */
    public void cancelCollection(final int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(MyCollectionActivity.this);
        builder.setTitle("提示:")
                .setMessage("确定取消收藏吗?")
                .setIcon(R.drawable.icon_my).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Goods goods = (Goods) adapter.getItem(position);
                //取消收藏
                int result=collection_db.delByUserAndGoods(goods.getUserId(),goods.getId());
                if(result != 0) {
                    Toast.makeText(MyCollectionActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                    // 下架后刷新页面：重新上查询并刷新
                    lists = getMyCollectionGoods();
                    adapter.setData(lists);
                }else{
                    Toast.makeText(MyCollectionActivity.this,"服务器出错!",Toast.LENGTH_SHORT).show();
                }
            }
        }).show();
    }

    /**
     * 查询用户收藏列表 t
     */
    private List<Goods> getMyCollectionGoods(){
        // 先根据用户id查询出商品id ， 用商品id查出商品详情
        collection_db=new CollectionDbHelper(getApplicationContext(),CollectionDbHelper.DB_NAME,null, Version.COLLECTION);
        List<Integer> ids=collection_db.getGoodsId(LoginUser.CURRENT_USER.getId());
        goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null,Version.GOODS);
        return goods_db.listByIds(ids);
    }

}
